#!/bin/bash
#=========================================
# Filename : arch-install.sh
# Filetype : Shell
# Author   : Colben
# Create   : 2015-11-21 00:39:40
#=========================================

# git user and its home directory
GIT_USER=''
GIT_HOME=''

OPTIONS='
    CreateGitUser
    SetGitUserHome
    ModifyMirrorlist
    InitInstall
    InstallTerminalSoftware
    InstallGnomeSoftware
    CloneGit
    ConfigVim
    ConfigBashTmuxGit
    HideAppIcon
    ConfigBackground
    Quit
'

# Add other change to a function below and
# append the function name to the options above ...

#function other_change {
#}

function Quit {
    exit 0
}

function CheckNetwork {
    local state=
    local status=
    echo 'Checking network connection ...'
    curl -sI --connect-timeout 8 https://www.baidu.com &> /dev/null
    status=$?
    [ 0 -eq $status ] && state='online' || state='offline'
    echo -e "\033[32mNetwork $state ...\033[0m"
    return $status
}

function CreateGitUser {
    local userName=
    while [ -z "$userName" ]; do
        read -p 'Enter the new user name:' userName
    done
    GIT_USER="$userName"
    if [ 'root' = "$GIT_USER" ]
    then
        mkdir -p /root/Git/ && GIT_HOME="/root"
        return
    fi
    useradd -s /bin/bash -d /home/$GIT_USER -m $GIT_USER
    mkdir -p /home/$GIT_USER/Git
    chown -R ${GIT_USER}.${GIT_USER} /home/$GIT_USER
    passwd $GIT_USER
    GIT_HOME="/home/$GIT_USER"
}

function SetGitUserHome {
    local userName=
    local userHome=
    while :; do
        read -p 'Enter the user keeping Git:' userName
        [ -z "$userName" ] && continue
        userHome=$(grep "^$userName" /etc/passwd 2>/dev/null|cut -d':' -f6 2>/dev/null)
        [ ! -d ${userHome:-'/dev/zero'}/Git ] && echo -n "$userName no Git, " && continue
        GIT_USER=$userName
        GIT_HOME=$userHome
        break
    done
}

function ModifyMirrorlist {
    sed -i '/^\[archlinuxcn\]$/,+2d' /etc/pacman.conf
    echo -e '[archlinuxcn]
SigLevel = Optional TrustAll
Include = /etc/pacman.d/mirrorlist.archlinuxcn' >> /etc/pacman.conf
    scp mirrorlist.archlinuxcn /etc/pacman.d/
    sed -i '/^\[blackarch\]$/,+2d' /etc/pacman.conf
    echo -e '[blackarch]
SigLevel = Optional TrustAll
Include = /etc/pacman.d/mirrorlist.blackarch' >> /etc/pacman.conf
    scp mirrorlist.blackarch /etc/pacman.d/
    scp mirrorlist /etc/pacman.d/
}

function InitInstall {
    CheckNetwork || return
    timedatectl set-local-rtc yes
    pacman -S networkmanager
    systemctl enable NetworkManager.service
}

function InstallTerminalSoftware {
    CheckNetwork || return
    pacman -S ctags cscope tmux expect git p7zip unrar zip unzip ntfs-3g
    pacman -S espeak hostapd dnsmasq openssh tcpdump vim cdrtools
    pacman -S tree ethtool openbsd-netcat arch-install-scripts
}

function InstallGnomeSoftware {
    CheckNetwork || return
    mkdir -p /usr/share/fonts/MSYH/TrueType/
    cp -r YaHei.Consolas.1.11b.ttf /usr/share/fonts/MSYH/TrueType/
    chown root.root /usr/share/fonts/* -R
    pacman -S wqy-zenhei ttf-liberation
    pacman -S gnome
    systemctl enable gdm.service
    pacman -S gvim file-roller freerdp mpv
    pacman -S vinagre flashplugin ibus-sunpinyin
    pacman -S faience-icon-theme gnome-tweak-tool
    pacman -S vertex-themes gnome-shell-extension-dash-to-dock
}

function CloneGit {
    local repos='
        https://gitee.com/colben/config.git
        https://gitee.com/colben/vim.git
        https://github.com/mlutfy/hidetopbar.git
        https://github.com/dmo60/CoverflowAltTab.git
    '
    local repoName=
    [ -z "$GIT_HOME" ] && echo 'We need set git user first' && return
    CheckNetwork || return
    cd $GIT_HOME/Git
    for repo in $repos; do
        echo -e "\n\033[33;1mgit clone\033[0m $repo"
        repoName=$(basename ${repo%.git})
        if [ -d $repoName/.git ]; then
            cd $repoName && git pull && cd ../
        else
            git clone $repo
        fi
    done
    chown `grep "^$GIT_USER" /etc/passwd|cut -d':' -f3,4` ../Git -R
}

function ConfigVim {
    [ -z "$GIT_HOME" ] && echo 'We need set git user first' && return
    cd $GIT_HOME/Git/
    [ 0 -ne $? ] && echo 'No vim-git found!' && return
    cp -r vim .vim && rm -rf .vim/.git
    rm -rf $GIT_HOME/.vim && cp -r .vim $GIT_HOME/
    chown `grep "^$GIT_USER" /etc/passwd|cut -d':' -f3,4` $GIT_HOME/.vim -R
    [ 'root' != "$GIT_USER" ] && rm -rf /root/.vim && cp -r .vim /root/
    rm -rf .vim
}

function ConfigBashTmuxGit {
    [ -z "$GIT_HOME" ] && echo 'We need set git user first' && return
    cd $GIT_HOME/Git/config
    [ 0 -ne $? ] && echo 'No bash-tmux-git found!' && return
    scp bash.bash_colben tmux.conf gitconfig /etc/
    sed -i '/colben/d' /etc/bash.bashrc
    echo 'source /etc/bash.bash_colben' >> /etc/bash.bashrc
}

function HideAppIcon {
    local file=
    cd /usr/share/applications
    for file in avahi-discover bssh bvnc flash-player-properties ibus-setup qv4l2 qvidcap nm-connection-editor; do
        if [ -f ${file}.desktop ]; then
            sed -i '/NoDisplay/d' ${file}.desktop
            echo 'NoDisplay=true' >> ${file}.desktop
        fi
    done
    for file in vim; do
        rm -f ${file}.desktop
    done
}

function ConfigBackground {
    [ -z "$GIT_HOME" ] && echo 'We need set git user first' && return
    cd $GIT_HOME/Git/archlinux
    scp -r V /boot/grub/themes/
    chown -R root.root /boot/grub/themes/V/
    sed -i '/^GRUB_BACKGROUND=/c#GRUB_BACKGROUND="/path/to/grub.tga"' /etc/default/grub
    sed -i '/GRUB_THEME=/cGRUB_THEME="/boot/grub/themes/V/theme.txt"' /etc/default/grub
    grub-mkconfig -o /boot/grub/grub.cfg
    cp -r desktop.png gdm.png terminal.jpg /usr/share/backgrounds/
    chown root.root /usr/share/backgrounds/ -R
}

function Main {
    local option=
    [ 0 -ne $UID ] && echo 'We need run this with root ...' && exit 1
    [ ! -x /usr/bin/pacman ] && echo 'Not Archlinux, quit ...' && exit 1
    select option in $OPTIONS; do
        [ -z $option ] && clear && continue
        echo -e "\n\033[36;47m ---- $option begin ---- \033[0m\n"
        $option
        echo -e "\n\033[36;47m ---- $option end   ---- \033[0m\n"
    done
}

# start here
Main

